﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace publish_test
{
    /// <summary>
    /// 工厂模式HttpHandle
    /// </summary>
    public class MyHttpHandlerFactory : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            #region 通过判断后缀实现简单工厂模式
            ////获取文件服务器物理路径
            //string path = context.Request.PhysicalPath;
            ////获取文件名后缀名
            //string exstention = Path.GetExtension(path).ToLower();

            //if (exstention == ".html")
            //{
            //    return new HtmlHttpHandler();
            //}
            //else if (exstention == ".xml")
            //{
            //    return new XMLHttpHandler();
            //}
            //else
            //{
            //    return null;
            //}
            #endregion

            #region 通过反射实现工厂模式
            string handlerName = url.Substring(url.LastIndexOf("/") + 1);
            string className = handlerName.Substring(0, handlerName.IndexOf("."));
            string fullClassName = "publish_test." + className;

            object h = null;

            Type type = Type.GetType(fullClassName);
            if (type==null)
            {
                return null;
            }
            // 采用动态反射机制创建相应的IHttpHandler实现类。
            h = Activator.CreateInstance(Type.GetType(fullClassName));

            return (IHttpHandler)h;

            #endregion
        }

        public void ReleaseHandler(IHttpHandler handler)
        {
            //throw new NotImplementedException();
        }
    }
}